package com.lm.javafx.example;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;

public class JavaScriptToJavaDemo extends Application {
    public static class JavaBridge {
        public void showMessage(String message) {
            System.out.println("JavaScript 调用 Java: " + message);
        }
    }

    @Override
    public void start(Stage primaryStage) {
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();



        webEngine.load("file:///E:\\lk\\springboot-example\\lm-javafx\\src\\main\\resources\\html\\activity.html");
        // 绑定 Java 对象到 JavaScript
        JSObject window = (JSObject) webEngine.executeScript("window");
        window.setMember("javaBridge", new JavaBridge());

        VBox root = new VBox(webView);
        Scene scene = new Scene(root, 800, 600);

        primaryStage.setTitle("JavaScript 调用 Java 示例");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
